//
//  PromptionViewController.m
//  MobileOffice
//
//  Created by jjkj on 16/10/19.
//  Copyright © 2016年 liaoningjujin. All rights reserved.
//

#import "PromptionViewController.h"
#import "PromptionTableViewCell.h"

@interface PromptionViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *_tableView;//列表
    NSArray *_dataArr;
    NSMutableArray *_swithArr;
}
@end

@implementation PromptionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setLeftBackNavigation];
    [self setNavigationTitle:@"系统提示设置"];
    [self setRightBackNavigation];
    [self creatView];
    [self getSwitchData];
    // Do any additional setup after loading the view.
}

#pragma mark -creatTableView
- (void)creatView{
    _dataArr=@[@"请假申请时限提示",@"请假审批规则提示",@"销假申请拍照提示",@"延假审批规则提示",@"销假/延假时限提示",@"审批处理时限提示",@"核销处理时限提示"];
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, IPHONE_HEIGHT-64)style:UITableViewStylePlain];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    _tableView.scrollEnabled=NO;
    //取消多余的行
    _tableView.tableFooterView = [UIView new];
    _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
}
#pragma mark -tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        return _dataArr.count;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellID=@"PromptionTableViewCell";
    PromptionTableViewCell *cell=[[PromptionTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    cell.titleLabel.text=_dataArr[indexPath.row];
    UILabel *line=[[UILabel alloc]initWithFrame:CGRectMake(0, 49, IPHONE_WIDTH, 1)];
    line.backgroundColor=BACKCOLOR_GRAY;
    [cell.contentView addSubview:line];
    if (_swithArr.count>0) {
        NSNumber *ison=_swithArr[indexPath.row];
        //如果以前保存过开关
        if (ison) {
            //如果开关打开
            if (ison.boolValue) {
                [cell.switchView setOn:YES];
            }else{
                [cell.switchView setOn:NO];
            }
        }else{
            [cell.switchView setOn:NO];
        }
    }
    cell.switchView.tag=200+indexPath.row;
    [cell.switchView addTarget:self action:@selector(switchIsChanged:) forControlEvents:UIControlEventValueChanged];
    return cell;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}

#pragma mark -switchDelegate
-(void)switchIsChanged:(UISwitch *)swith
{
    if ([swith isOn]){//保存开关状态
        [[NSUserDefaults standardUserDefaults]setObject:[NSNumber numberWithBool:YES] forKey:[NSString stringWithFormat:@"switch%ld",swith.tag-200]];
    } else {
        [[NSUserDefaults standardUserDefaults]setObject:[NSNumber numberWithBool:NO] forKey:[NSString stringWithFormat:@"switch%ld",swith.tag-200]];
    }
}
//获取保存的开关状态
- (void)getSwitchData{
    _swithArr=[NSMutableArray new];
    for (NSUInteger i=0; i<_dataArr.count; i++) {
        NSNumber *isOn=[[NSUserDefaults standardUserDefaults]objectForKey:[NSString stringWithFormat:@"switch%ld",i]];
        //如果不是第一次打开
        if (isOn) {
            [_swithArr addObject:isOn];
        }else{
            [_swithArr addObject:[NSNumber numberWithBool:NO]];
        }

    }
    [_tableView reloadData];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
